# coding = utf8
import numpy

#定义数据类，用来存储对象
class MyData():
    def __init__(self, aaa, bbb, ccc):
        self.aaa = aaa
        self.bbb = bbb
        self.ccc = ccc


#距离计算类
class CalculateDistance():
    #构造器
    def __init__(self, obj1, obj2):
        self.obj1 = obj1
        self.obj2 = obj2

    # 计算简单匹配系数
    def calMatchNum(self):
        list1 = self.obj1
        list2 = self.obj2
        f00 = 0
        f01 = 0
        f10 = 0
        f11 = 0
        nn = len(list1)
        for i in range(0, nn):
            if list1[i]==0 and list2[i]==0:
                f00 += 1
            if list1[i]==0 and list2[i]==1:
                f01 += 1
            if list1[i]==1 and list2[i]==0:
                f10 += 1
            if list1[i]==1 and list2[i]==1:
                f11 += 1
        return round((f00+f11)/(f00+f11+f01+f10), 2)

    #计算欧氏距离
    def calEuclideanDistance(self):
        v1 = numpy.array(self.obj1)
        v2 = numpy.array(self.obj2)
        dist = numpy.sqrt(numpy.sum(numpy.square(v1 - v2)))
        return round(dist, 2)

    #计算余弦距离
    def calCOSDistance(self):
        dist1 = 1 - numpy.dot(self.obj1, self.obj2) / (numpy.linalg.norm(self.obj1) * numpy.linalg.norm(self.obj2))
        return round(dist1, 2)


if __name__ == '__main__':
    m1 = MyData([0, 0, 0, 1, 1, 0], [3, 5, 2, 6, 7], [4, 0, 3, 2, 7])
    m2 = MyData([1, 1, 0, 1, 0, 1], [1, 2, 3, 4, 5], [0, 1, 5, 8, 3])
    m3 = MyData([1, 0, 0, 0, 1, 0], [9, 8, 6, 4, 2], [6, 7, 9, 3, 4])

    #计算对象12之间的简单匹配系数
    ca12 = CalculateDistance(m1.aaa, m2.aaa)
    da12 = ca12.calMatchNum()
    print(1/(1+da12))

    #计算对象12之间的欧氏距离
    c12 = CalculateDistance(m1.bbb, m2.bbb)
    db12 = c12.calEuclideanDistance()
    print(1/(1+db12))

    #计算对象12之间的余弦距离
    cc12 = CalculateDistance(m1.ccc, m2.ccc)
    cd12 = cc12.calCOSDistance()
    print(1/(1+cd12))

    #计算对象13之间的简单匹配系数
    ca13 = CalculateDistance(m1.aaa, m3.aaa)
    da13 = ca13.calMatchNum()
    print(1/(1+da13))

    #计算对象13之间的欧氏距离
    c13 = CalculateDistance(m1.bbb, m3.bbb)
    db13 = c13.calEuclideanDistance()
    print(1/(1+db13))

    #计算对象13之间的余弦距离
    cc13 = CalculateDistance(m1.ccc, m3.ccc)
    cd13 = cc13.calCOSDistance()
    print(1/(1+cd13))

    #计算对象23之间的简单匹配系数
    ca23 = CalculateDistance(m2.aaa, m3.aaa)
    da23 = ca13.calMatchNum()
    print(1/(1+da23))

    #计算对象23之间的欧氏距离
    c23 = CalculateDistance(m2.bbb, m3.bbb)
    db23 = c23.calEuclideanDistance()
    print(1/(1+db23))

    #计算对象23之间的余弦距离
    cc23 = CalculateDistance(m2.ccc, m3.ccc)
    cd23 = cc23.calCOSDistance()
    print (1/(1+cd23))


    s12 = 0.3*(1/(1+da12)) + 0.2*(1/(1+db12)) + 0.5*(1/(1+cd12))
    print("1与2的相似性为",  round(s12, 2))

    s13 = 0.3*(1/(1+da13)) + 0.2*(1/(1+db13)) + 0.5*(1/(1+cd13))
    print("1与3的相似性为",  round(s13, 2))

    s23 = 0.3*(1/(1+da23)) + 0.2*(1/(1+db23)) + 0.5*(1/(1+cd23))
    print("2与3的相似性为",  round(s23, 2))